<?php
// $Id: table_hotel_hall.php 399 2012-05-27 12:12:58Z tomqin@gmail.com $

class Table_Hotel_Hall_Module{
    private $user = null;

    public function __construct($user){
        $this->user = $user;
    }

    public function check_user_right($shop_id){
        if($this->user->status == USER_ADMIN){
            return true;
        }elseif($this->user->status == USER_BUSINESS){
            $uid = $this->user->uid;
            $shop_id = intval($shop_id);
            return Db::getFirst("SELECT count(shop_id) FROM `biz_shop` WHERE admin_uid = '$uid' AND shop_id = '$shop_id'");
        }else{
            return false;
        }
    }

    public function get_data($shop_id, $hall_id){
        return Db::getRow("SELECT * FROM `biz_hotel_hall` WHERE hall_id = '{$hall_id}' AND shop_id = '{$shop_id}'");
    }

    public function get_list($shop_id){
        return Db::getAll("SELECT * FROM `biz_hotel_hall` WHERE shop_id = '{$shop_id}' ORDER BY ordnum DESC");
    }

    public function search(&$search, &$order, &$page, &$total, $eachpage = 10){
        return null;
    }

    public function insert(array $post){
        $data = $this->get_fields($post);
        return Db::insert('biz_hotel_hall', $data);
    }

    public function update($shop_id, $hall_id, array $post){
        $data = $this->get_fields($post);
        unset($data['shop_id']);
        return Db::update('biz_hotel_hall', $data, array('hall_id' => $hall_id, 'shop_id' => $shop_id));
    }

    public function sort($shop_id, array $array){
        $stmt = Db::prepare('UPDATE `biz_hotel_hall` SET ordnum = ? WHERE hall_id = ? AND shop_id = ?');
        $stmt->bind_param('iii', $ordnum, $hall_id, $shop_id);
        foreach($array as $hall_id => $ordnum){
            $stmt->execute();
        }
    }

    public function delete($shop_id, $hall_id){
        $shop_id = intval($shop_id);
        $hall_id = intval($hall_id);
        Db::query("DELETE FROM `biz_hotel_hall` WHERE hall_id = '{$hall_id}' AND shop_id = '{$shop_id}'");
    }

    protected function get_fields($post){
        return array(
            'shop_id'    => array_get_int($post, 'shop_id'),
            'name'       => array_get_string($post, 'name'),
            'picture'    => array_get_string($post, 'picture'),
            'floor'      => array_get_int($post, 'floor'),
            'table'      => array_get_int($post, 'table'),
            'height'     => array_get_double($post, 'height'),
            'square'     => array_get_int($post, 'square'),
            'shape'      => array_get_string($post, 'shape'),
            'description'=> array_get_string($post, 'description'),
            'ordnum'     => array_get_int($post, 'ordnum'),
        );
    }
}
?>